
package com.example.ymm.base;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.ymm.dto.HttpStatus;
import com.example.ymm.model.UserLogin;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.util.SecurityUtil;
import com.example.ymm.util.UtilValidate;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hupeng
 * @since 2019-10-16
 */
@Slf4j
@SuppressWarnings("unchecked")
public abstract class BaseServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> implements BaseService<T> {

    public static final String DESC="DESC";

    public static final String ASC="ASC";



    public static Map<String, Object> error(String message){
        Map<String, Object> resultMap = new LinkedHashMap<String, Object>();
        resultMap.put("status", HttpStatus.ERROR_01.getStatus());
        resultMap.put("message", message);
        return resultMap;
    }

    public static Map<String, Object> success(String message,Object o){
        Map<String, Object> resultMap = new LinkedHashMap<String, Object>();
        resultMap.put("status", HttpStatus.SUCCESS.getStatus());
        resultMap.put("message", message);
        resultMap.put("result", o);
        return resultMap;
    }

    protected void getPage(YmPageDto pageDto) {
        String order = null;
//        if(UtilValidate.isNotEmpty(pageDto.getSortColumn())){
//            order+=" order by "+pageDto.getSortColumn()+" "+pageDto.getSortOrder();
//        }
        PageHelper.startPage(pageDto.getPage(), pageDto.getPageSize(), order);
    }


    public UserLogin getUserLogin(){
        return SecurityUtil.getUserLogin();
    }

    public String getUserRole(){
        String role=null;
        UserLogin userLogin = getUserLogin();
        List<String> roleList = userLogin.getRoleList();
        if(UtilValidate.isNotEmpty(roleList)){
            role=roleList.get(0);
        }
        return role;
    }

    public String getUserId(){
        return SecurityUtil.getUserId();
    }

    public String getUserName(){
        return getUserLogin().getUserName();
    }

    public String getUserNickName(){
        return SecurityUtil.getUserNickName();

    }

}
